package com.hanson.global;

import com.hanson.common.api.CommonResult;
import com.hanson.common.api.ResultCode;
import com.hanson.common.exception.APIException;
import com.hanson.common.exception.DmallException;
import com.hanson.common.exception.RateLimitException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: 全局异常处理类
 * @classname: ddd
 * @author: Hanson
 * @create: 2021/01/08
 **/
@Slf4j
@RestControllerAdvice(basePackages = "com.hanson.controller")
public class ExceptionControllerAdvice {

    @ExceptionHandler(APIException.class)
    public CommonResult<String> globalExceptionHandler(APIException e) {
        // 然后提取错误提示信息进行返回
        log.error("统一异常返回捕获到APIException异常啦：",e);
        return new CommonResult<>(e.getResultCode(),null, e.getMsg());
    }

    @ExceptionHandler(DmallException.class)
    public CommonResult<String> globalExceptionHandler(DmallException e) {
        // 然后提取错误提示信息进行返回
        log.error("统一异常返回捕获到DmallException异常啦：",e);
        return new CommonResult<>(ResultCode.FAILED,null,e.getMessage());
    }

    @ExceptionHandler(RateLimitException.class)
    public CommonResult<String> globalExceptionHandler(RateLimitException e) {
        // 然后提取错误提示信息进行返回
        log.error("统一异常返回捕获到RateLimitException异常啦：");
        return new CommonResult<>(ResultCode.FAILED,null,e.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public CommonResult<String> globalExceptionHandler(Exception e) {
        // 然后提取错误提示信息进行返回
        log.error("统一异常返回捕获到Exception异常啦：",e);
        return new CommonResult<>(ResultCode.FAILED,null,"服务异常！");
    }


}
